'''
Created on Apr 3, 2014

@author: sshadmin
'''
from model.footyroom import Main ,Match ,Playwire,User, PageLog,News,Previews,Feedback
from flask import Flask
from flask import Blueprint
from flask import request
import json
import traceback
from datetime import datetime
from logger import getLogger
log=getLogger("footyroom")

app = Blueprint('footy_service', __name__)

@app.route('/footy/register' , methods=['GET'])    
def footy_register():
    try:
        device_id=request.args.get("device_id")
        gcm_key=request.args.get("gcm_key")
        device_name=request.args.get("device_name")
        package_version=request.args.get("package_version")
        email=request.args.get("email")
        android_version=request.args.get("android_version")
        ip_address=request.remote_addr
        log.info(ip_address)
        u=User(device_id=device_id,gcm_key=gcm_key,package_version=package_version,email=email,android_version=android_version,device_name=device_name)
        u.save()
        return json.dumps(""),200
    except:
        log.error(traceback.format_exc())
        return json.dumps(traceback.format_exc()), 400    
@app.route('/footy' , methods=['GET'])     
def footy_main():
    try:
        device_id=request.args.get("device_id")
        m=Main()
        matches=[]
        try:
            matches=m.fetch()
        except:
            log.error(traceback.format_exc())
        ret_matches=[]
        for match in matches:
            ret_matches.append(match.to_dic())
        u=PageLog(user=device_id,page="main",created=datetime.now())
        try:
            u.save()
        except:
            pass
        return json.dumps(ret_matches),200
    except:
        log.error(traceback.format_exc())
        return json.dumps(traceback.format_exc()), 400
@app.route('/footy/match' , methods=['GET']) 
def footy_match():
    try:
        url=request.args.get("url")
        user=request.args.get("device_id")
        log.info(url)
        print url
        m=Match(url)
        try:
            m.fetch()
        except:
            log.error(traceback.format_exc())
        u=PageLog(user=user,page=url,created=datetime.now())
        try:
            u.save()
        except:
            log.info(traceback.format_exc())
        return json.dumps(m.to_dic()),200
    except:
        log.error(traceback.format_exc())
        return json.dumps(traceback.format_exc()), 400
@app.route('/footy/news' , methods=['GET']) 
def footy_news():
    try:
        m=News()
        try:
            m.fetch()
        except:
            log.error(traceback.format_exc())
        return json.dumps(m.to_dic()),200
    except:
        log.error(traceback.format_exc())
        return json.dumps(traceback.format_exc()), 400
@app.route('/footy/previews' , methods=['GET']) 
def footy_previews():
    try:
        m=Previews()
        m.fetch()
        return json.dumps(m.to_dic()),200
    except:
        log.error(traceback.format_exc())
        return json.dumps(traceback.format_exc()), 400
@app.route('/footy/feedback' , methods=['GET']) 
def footy_feedback():
    try:
        feedback=request.args.get("feedback")
        device_id=request.args.get("device_id")
        m=Feedback(device_id=device_id,feedback=feedback)
        m.save()
        return json.dumps([]),200
    except:
        log.error(traceback.format_exc())
        return json.dumps(traceback.format_exc()), 400